<?php

namespace app\admin\controller;

use think\Log;

class Article extends Admin
{
    public function __construct()
    {
        parent::__construct();
        $this->model = "Article";
        $this->modelDb = model("Article");
        $this->assign("model", $this->model);
    }

    public function index()
    {
        $getData = input('param.');
        $whereData = '';
        if ($getData) {
            foreach ($getData as $k => $v) {
                if (!$whereData) {
                    $whereData .= $k . '=' . $v;
                } else {
                    $whereData .= '&' . $k . '=' . $v;
                }
            }
        }
        $dealerlist = [];
        if ($this->uinfo['roleid'] == '1' || $this->uinfo['roleid'] == '4') {
            $dealerlist = model('AdminUser')->whereOr('roleid', '1')->whereOr('roleid', '4')->whereOr('roleid', '6')->column('userid, realname');
        }
        $uinfo = $this->uinfo;
        $this->assign('keywords', isset($getData['keywords']) ? $getData['keywords'] : '');
        $this->assign("whereData", $whereData);
        $this->assign("uinfo", $uinfo);
        $this->assign("dealerlist", $dealerlist);
        return $this->fetch();
    }

    public function get_where_map()
    {
        $dealerid = input('param.dealerid');
        $keywords = input('param.keywords');
        $whereDatas = [];
        if ($dealerid) {
            $whereDatas['dealerid'] = $dealerid;
        } else {
            if ($this->uinfo['userid'] !== 1 and $this->uinfo['userid'] !== 2) {
                $whereDatas['dealerid'] = array(['=', 2], ['=', $this->uinfo['userid']], 'or');
            }
        }
        if ($keywords) {
            $whereDatas['title|content'] = ['like', "%$keywords%"];
        }
        Log::record('eeee' . var_export($whereDatas, true), 'info');
        return $whereDatas;
    }

    // 打开添加消息的界面
    public function add()
    {
        return $this->fetch();
    }

    // 保存新添加的消息
    public function save_article_data()
    {
        $postData = input('post.postData');
        parse_str($postData, $dataarr);
        $title = $dataarr['title'];
        if (!$title) {
            return json(['status' => 0, 'msg' => '请输入文章标题']);
        }
        $content = $dataarr['content'];
        if (!$content) {
            return json(['status' => 0, 'msg' => '请输入文章内容']);
        }
        $cover = $dataarr['cover'];
        if (!$cover) {
            return json(['status' => 0, 'msg' => '请上传封面图片']);
        }
        $data = [
            'title' => $title,
            'cover' => $cover,
            'content' => $content,
            'datetime' => time(),
            'updatetime' => time(),
            'author' => $dataarr['author'],
            'dealerid' => $this->uinfo['userid'],
        ];
        $this->modelDb->save_data($data);
        return json(['status' => 1, 'msg' => '操作成功']);
    }

    public function image() {
        $cover = input('param.cover');
//        $info = model($this->model)->get_data(['item_id' => $id]);
        $this->assign('cover', $cover);
        return $this->fetch();
    }

    public function test()
    {
        $where = [];
        return $this->fetch();
    }
}